﻿#include"stdafx.h"
#include"SQLexpression.h"


bool SqlEx::ForAddField()
{
	string db = CStringTostring(pSocket->cur_db);
	if (db == "") {
		pSocket->SendMSG("Please choose a database !");
		return 0;
	}

	string msg = "";
	string tname = "";
	vector<string>field_info;
	regex reg1("(?<=alter table ).+?(?= add)");
	regex reg2("(?<=add ).+?(?=END)");
	smatch sm;

	if (regex_search(Ex, sm, reg1)) {
		tname = sm.str();
	}
	else {
		pSocket->SendMSG("检查alter table 和add语句之间是否存在错误！！！");
		return false;
	}

	if (!authority->Auth_Judge(user_host, db, tname)) {
		pSocket->SendMSG("You do not have permission to alter the table !");
		return 0;
	}
	if (regex_search(Ex, sm, reg2)) {
		string str = sm.str();
		vector<string> temp = split(str, " ");
		if (temp.size() == 1) {
			pSocket->SendMSG("检查语句是否存在错误！！！");
			return false;
		}
		string type = "", param = "";
		if ((type = isType(temp[1], param)) == "-1") {
			msg = "不支持的类型" + temp[1];
			pSocket->SendMSG(msg);
			return false;
		}
		field_info.push_back(temp[0]);
		field_info.push_back(type);
		field_info.push_back(param);

		if (str.find("primary key") != -1) {
			field_info.push_back("6");
		}
		if (str.find("unique") != -1) {
			field_info.push_back("7");
		}
		if (str.find("not null") != -1) {
			field_info.push_back("8");
		}
		if (str.find("auto_increase") != -1) {
			field_info.push_back("9");
		}

	}
	else {
		pSocket->SendMSG("检查add之后语句是否存在错误！！！");
		return false;
	}
	serverDB->Table_Alter_Add(tname.c_str(), db, field_info, msg);
	pSocket->SendMSG(msg);
	return true;
}

bool SqlEx::ForDropField()
{
	string db = CStringTostring(pSocket->cur_db);
	if (db == "") {
		pSocket->SendMSG("Please choose a database !");
		return 0;
	}

	string msg = "", tname = "", field = "";
	regex reg1("(?<=alter table ).+?(?= drop)");
	regex reg2("(?<=drop ).+?(?=END)");
	smatch sm;
	if (regex_search(Ex, sm, reg1)) {
		tname = sm.str();
	}
	else {
		pSocket->SendMSG("检查alter table 和drop语句之间是否存在错误！！！");
		return false;
	}
	if (!authority->Auth_Judge(user_host, db, tname)) {
		pSocket->SendMSG("You do not have permission to alter the table !");
		return 0;
	}
	if (regex_search(Ex, sm, reg2)) {
		field = sm.str();
	}
	else {
		pSocket->SendMSG("检查drop语句之后是否存在错误！！！");
		return false;
	}
	serverDB->Table_Alter_Drop(tname.c_str(), db, field, msg);
	pSocket->SendMSG(msg);
	return true;
}

bool SqlEx::ForModifyField()
{
	string db = CStringTostring(pSocket->cur_db);
	if (db == "") {
		pSocket->SendMSG("Please choose a database !");
		return 0;
	}

	string msg = "", tname = "";
	vector<string>field_info;
	regex reg1("(?<=alter table ).+?(?= modify)");
	regex reg2("(?<=modify ).+?(?=END)");
	smatch sm;
	if (regex_search(Ex, sm, reg1)) {
		tname = sm.str();
	}
	else {
		pSocket->SendMSG("检查alter table 和modify语句之间是否存在错误！！！");
		return false;
	}
	if (!authority->Auth_Judge(user_host, db, tname)) {
		pSocket->SendMSG("You do not have permission to alter the table !");
		return 0;
	}
	if (regex_search(Ex, sm, reg2)) {
		string str = sm.str();
		vector<string> temp = split(str, " ");
		if (temp.size() == 1) {
			pSocket->SendMSG("检查语句是否存在错误！！！");
			return false;
		}
		string type = "", param = "";
		if ((type = isType(temp[1], param)) == "-1") {
			msg = "不支持的类型" + temp[1];
			pSocket->SendMSG(msg);
			return false;
		}
		if (type != "4") {
			pSocket->SendMSG("Column does not allow modified  !");
			return false;
		}
		field_info.push_back(temp[0]);
		field_info.push_back(type);
		field_info.push_back(param);

		if (str.find("primary key") != -1) {
			field_info.push_back("6");
		}
		if (str.find("unique") != -1) {
			field_info.push_back("7");
		}
		if (str.find("not null") != -1) {
			field_info.push_back("8");
		}
		if (str.find("auto_increase") != -1) {
			field_info.push_back("9");
		}

	}
	else {
		pSocket->SendMSG("检查modify语句之后是否存在错误！！！");
		return false;
	}
	serverDB->Table_Alter_Modify(tname.c_str(),db,field_info,msg);
	pSocket->SendMSG(msg);

	return true;
}
